package com.example.check.web.controller;

import com.example.check.entity.RoleEntity;
import com.example.check.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import static com.example.common.AssertThrowUtil.$;

/**
 * Created by Tony Yao on 2017/7/15.
 */
@Controller
public class RoleController {
    @Resource
    private RoleService rfun;

    @RequestMapping(method= RequestMethod.GET,path="/searchRole.do")
    public String searchRoleView(String field, String code, HttpServletRequest request,
                                 Model model) throws Exception {
        ArrayList<RoleEntity> roleList = null;
        if (code == null) {
            roleList = rfun.searchAllRole();
        } else {
            roleList = rfun.SearchRoleLikeByField(field, code);
        }
        model.addAttribute("searchRoleList",roleList);
//        HttpSession session = request.getSession();
//        session.setAttribute("userList",userList);
        return "searchRole";
    }

    @RequestMapping(method= RequestMethod.GET,path="/deleteRole.do")
    public String deleteRoleView(int id, HttpServletRequest request ,Model model)throws Exception{
        ArrayList<RoleEntity> roleList = null;
        rfun.deleteRoleById(id);
        roleList = rfun.searchAllRole();
        HttpSession session = request.getSession();
        session.setAttribute("roleList",roleList);
        request.setAttribute("message","删除角色成功！");
        return "manageRole";
    }

    @RequestMapping(method= RequestMethod.POST,path="/editRole.do")
    public String editRole(int id, String level, String name,String perStr, String description,
                           HttpServletRequest request, Model model){
        try {
            level =$("等级不能为空！",level);
            name =$("名字不能为空！",name);

            RoleEntity r = new RoleEntity(id,level,name,perStr,description);
            System.out.println("权限串" + perStr);
            rfun.editRole(r);

            ArrayList<RoleEntity> roleList = rfun.searchAllRole();
            HttpSession session = request.getSession();
            session.setAttribute("roleList",roleList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("message","修改角色成功！");
        return "manageRole";
    }

    @RequestMapping(method= RequestMethod.GET,path="/editRoleView.do")
    public String editRoleView(int id, HttpServletRequest request){
        try {
            ArrayList<RoleEntity> chosenRole = rfun.SearchRoleById(id);
            String str = chosenRole.get(0).getPermission();
            String[] perOfRole = str.split(",");

            HttpSession session = request.getSession();
            session.setAttribute("chosenRole",chosenRole.get(0));
            session.setAttribute("perOfRole",perOfRole);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "editRole";
    }

    @RequestMapping(method= RequestMethod.POST,path="/addRole.do")
    public String addRole(String level, String name, String perStr, String description, HttpServletRequest request){
        try{
            level =$("等级不能为空！",level);
            name =$("名字不能为空！",name);
            RoleEntity r = new RoleEntity(-1,level,name,perStr,description);
            rfun.insertRole(r);

            ArrayList<RoleEntity> roleList = rfun.searchAllRole();
            HttpSession session = request.getSession();
            session.setAttribute("roleList",roleList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("message","添加角色成功！");
        return "manageRole";
    }

    @RequestMapping(method= RequestMethod.GET,path="/addRoleView.do")
    public String addRoleView(){
        return "addRole";
    }


    @RequestMapping(method= RequestMethod.GET,path="/manageRole.do")
    public String manageRoleView(String field, String code, Model model, HttpServletRequest request) throws Exception{
        ArrayList<RoleEntity> roleList = null;
        if (code == null) {
            roleList = rfun.searchAllRole();
        } else {
            roleList = rfun.SearchRoleLikeByField(field, code);
        }
        HttpSession session = request.getSession();
        session.setAttribute("roleList",roleList);

        return "manageRole";
    }


}
